package com.cty.servlet;

import com.cty.service.HeroService;
import com.cty.service.impl.HeroServiceImpl;
import com.cty.vo.Hero;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Auther: cty
 * @Date: 2020/2/14 21:07
 * @Description:
 * @version: 1.0
 */
@WebServlet(name = "HeroServlet")
public class HeroServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求编码格式
        req.setCharacterEncoding("utf-8");
        //设置响应编码格式
        resp.setContentType("text/javascript;charset=utf-8");
        //获取请求信息
        String name = req.getParameter("name");
//        String name = "孙悟空";
        System.out.println("用户请求信息为：" + name);
        //处理请求信息
        //获取业务层对象
        HeroService us = new HeroServiceImpl();
        //处理业务
        Hero u = us.getHeroInfoService(name);
        System.out.println("查询结果为:\n" + u);
        //响应处理结果
        resp.getWriter().write(new Gson().toJson(u));
    }
}
